To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 20 seconds to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1
👀 Reading hidden code
Enter cell code...
67.6 μs

@eval for faster array check

👀 Reading hidden code
187 μs
👀 Reading hidden code
using BenchmarkTools
65.4 ms
👀 Reading hidden code
xs = [1,2,3]
12.6 μs

Base case

👀 Reading hidden code
182 μs
issmall1 (generic function with 1 method)
👀 Reading hidden code
function issmall1(x)
xxs
end
361 μs
BenchmarkTools.Trial: 10000 samples with 997 evaluations per sample.
 Range (min … max):  20.962 ns … 44.607 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     21.615 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   21.811 ns ±  1.253 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

      █▇                                                      ▁
  ▃▁▁▁██▃▁▃▃▃▁▁▃▁▁▁▁▄▆▄▅▁▄▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▃▁▃▁▁▁▁▁▁▃▅▇██ █
  21 ns        Histogram: log(frequency) by time        29 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
👀 Reading hidden code
@benchmark issmall1(5)
1.8 s
BenchmarkTools.Trial: 10000 samples with 997 evaluations per sample.
 Range (min … max):  18.811 ns … 49.128 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     18.832 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   19.064 ns ±  1.446 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █                                                           ▁
  █▁▁▄▁▃█▁▃▁▁▁▁▄▄▄▅▅▁▃▁▁▁▃▃▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▃▇██▇ █
  18.8 ns      Histogram: log(frequency) by time      26.3 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
@benchmark issmall1(2)
👀 Reading hidden code
801 ms

With @eval

👀 Reading hidden code
182 μs
issmall2 (generic function with 1 method)
@eval function issmall2(x)
$(or_sequence(:(x == $v) for v in xs))
end
👀 Reading hidden code
3.5 ms
Error message

UndefVarError: issmall2 not defined

Stack trace

Here is what happened, the most recent locations are first:

  1. var"##core#323"()
  2. var"##sample#324"(::Tuple{}, __params::BenchmarkTools.Parameters)
  3. _lineartrial(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; maxevals::Int64, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
  4. _lineartrial(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters)
  5. #invokelatest#2
  6. invokelatest
  7. #lineartrial#46
  8. lineartrial
  9. tune!(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, verbose::Bool, pad::String, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
  10. tune!
  11. macro expansion
  12. Show more...
¯\_(ツ)_/¯
@benchmark issmall2(2)
👀 Reading hidden code
---
Error message

UndefVarError: issmall2 not defined

Stack trace

Here is what happened, the most recent locations are first:

  1. var"##core#331"()
  2. var"##sample#332"(::Tuple{}, __params::BenchmarkTools.Parameters)
  3. _lineartrial(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; maxevals::Int64, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
  4. _lineartrial(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters)
  5. #invokelatest#2
  6. invokelatest
  7. #lineartrial#46
  8. lineartrial
  9. tune!(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, verbose::Bool, pad::String, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
  10. tune!
  11. macro expansion
  12. Show more...
@benchmark issmall2(5)
👀 Reading hidden code
---
or_sequence (generic function with 1 method)
or_sequence(exprs) = foldl(Iterators.reverse(exprs)) do e, next
:($(next) || $(e))
end
👀 Reading hidden code
834 μs
:(1 || (2 || 3))
or_sequence(xs)
👀 Reading hidden code
12.4 μs